document.ready(function() {
    var loginFlag = getStorage('helper_autologin');
    if (loginFlag != null && loginFlag != '') {
        getItem('isLogin').checked = true;

        setValue('username', loginFlag.split('@')[0]);
        setValue('password', loginFlag.split('@')[1]);

        login();
    }
});

function login() {
    if (isEmpty('username') || isWicked('username')) {
        showTip('用户名格式错误');
        return;
    }
    if (isEmpty('password') || isWicked('password')) {
        showTip('密码格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'login';
    param.username = getAjaxValue('username');
    param.password = getAjaxValue('password');

    sendAjax(param, function(result) {
        setStorage('user_token', result.token);
        delStorage('duty_token');

        if (getItem('isLogin').checked) {
            setStorage('helper_autologin', getValue('username') + '@' + getValue('password'));
        }

        toUrl('console.php?m=account&c=main');
    });
}